import { Component, ElementRef, EventEmitter, Injector, Output, ViewChild, Input, OnInit } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { TaskForModelDto, TaskServiceProxy, WorkflowServiceProxy, UserServiceProxy, ExecutionPointerDto, OutsourcingWorkFlowServiceProxy } from '@shared/service-proxies/service-proxies';
import * as _ from 'lodash';
import { Paginator } from 'primeng/components/paginator/paginator';
import { Table } from 'primeng/components/table/table';
import { PrimengTableHelper } from 'shared/helpers/PrimengTableHelper';

@Component({
  selector: 'OrdinaryCrucialAudit',
  templateUrl: './OrdinaryCrucialAudit.component.html',
  styleUrls: ['./OrdinaryCrucialAudit.component.css']
})
export class OrdinaryCrucialAuditComponent extends AppComponentBase implements OnInit {

  @ViewChild('paginator') paginator: Paginator;
  @ViewChild('taskdataTable') taskdataTable: Table;
  @ViewChild('dataTable') dataTable: Table;

  @Input() workflowid: any;
  @Output('out') out = new EventEmitter();

  saving = false;
  Outsourcings;
  task: TaskForModelDto = new TaskForModelDto();
  workflow;
  detailsprimengTableHelper: PrimengTableHelper;

  constructor(injector: Injector,
    private _taskServiceProxy: TaskServiceProxy,
    private _outSourcingworkflowServiceProxy: OutsourcingWorkFlowServiceProxy,
    private _userServiceProxy: UserServiceProxy,
    private _workflowService: WorkflowServiceProxy) {
    super(injector);
    this.detailsprimengTableHelper = new PrimengTableHelper();
  }

  ngOnInit(): void {
    console.log(this.workflowid);
    this.initForm(); 
  }
  initForm(): void {
    this._outSourcingworkflowServiceProxy.getOutsourcingWorkFlow(this.workflowid).subscribe(dto => {
      this.workflow = dto.workFlowDto;
      this.Outsourcings = dto.outSourcingDtos;
      this.primengTableHelper.totalRecordsCount = this.Outsourcings.length;
      this.primengTableHelper.records = this.Outsourcings;
      this.primengTableHelper.hideLoadingIndicator();

      _.forEach(this.Outsourcings, viewItem => {
        this._taskServiceProxy.getTaskForEdit(viewItem.taskForModelId)
          .subscribe(userResult => {
            viewItem.taskCode = userResult.displayName;
            viewItem.taskName = userResult.code;
            viewItem.picNo = userResult.picNo;
          });
      });
    });

  }

  audityes(): void {
    console.log('同意');
    this._outSourcingworkflowServiceProxy.crucialOutsourcingFlow_execute(this.workflowid, 'yes').subscribe(dto => {
      var evt = document.createEvent("HTMLEvents");
      evt.initEvent("callBackFromIframe", false, false);
      parent.window.document.dispatchEvent(evt);
      this.out.emit(evt);
    });
  }

  auditno(): void {
    this._outSourcingworkflowServiceProxy.crucialOutsourcingFlow_execute(this.workflowid, 'no').subscribe(dto => {
      console.log(dto);
    });
  }
  
  getAuditName(item: ExecutionPointerDto) {
    console.log(item)
    return item.extensionAttributes.Prompt;
  }
}
